﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DefaultWebDriverEventListener.cs" company="">
// </copyright>
// <summary>
//   The default web driver event listener.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Selenium.EventListener
{
    using System;
    using System.Drawing.Imaging;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Support.Events;

    /// <summary>
    ///     The default web driver event listener.
    /// </summary>
    public class DefaultWebDriverEventListener
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultWebDriverEventListener"/> class.
        /// </summary>
        /// <param name="webDriver">
        /// The web driver.
        /// </param>
        public DefaultWebDriverEventListener(IWebDriver webDriver)
        {
            this.WebEventDriver = new EventFiringWebDriver(webDriver);

            // Add Exception Listen Event
            this.WebEventDriver.ExceptionThrown += this.TakeScreenshotOnException;
            this.WebEventDriver.Navigating += this.LogWhenNavigating;
            this.WebEventDriver.ElementClicking += this.LogElelmentClicking;
            this.WebEventDriver.ScriptExecuted += this.LogScriptExecuted;
            this.WebEventDriver.ElementValueChanged += this.LogElementValueChanged;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the screen shot file name.
        /// </summary>
        public string ScreenShotFileName { get; set; }

        /// <summary>
        ///     Gets or sets the web event driver.
        /// </summary>
        public EventFiringWebDriver WebEventDriver { get; set; }

        #endregion

        #region Methods

        private void LogElelmentClicking(object sender, WebElementEventArgs e)
        {
            Console.WriteLine("Clicking on {0} {1}", e.Element.TagName, e.Element.Text);
        }

        private void LogElementValueChanged(object sender, WebElementEventArgs e)
        {
            Console.WriteLine("Change value of {0} ....", e.Element.TagName);
        }

        private void LogScriptExecuted(object sender, WebDriverScriptEventArgs e)
        {
            Console.WriteLine("Executed script on {0}", e.Script);
        }

        private void LogWhenNavigating(object sender, WebDriverNavigationEventArgs e)
        {
            Console.WriteLine("Navigating to {0}....", e.Url);
        }

        private void TakeScreenshotOnException(object sender, WebDriverExceptionEventArgs e)
        {
            Console.WriteLine("Taking Screen Shot");
            string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
            this.ScreenShotFileName = "Exception-" + timestamp + ".png";
            this.WebEventDriver.GetScreenshot().SaveAsFile(this.ScreenShotFileName, ImageFormat.Png);
        }

        #endregion
    }
}